package org.example;

import org.junit.Test;

import java.util.List;
import java.util.Set;

import static org.junit.Assert.*;

public class UserDaoTest {
    //这些测试写的都OK
    @Test
    public void getAll() {
    }

    @Test
    public void delete() {
        UserDao userDao = new UserDao();
        User user1 = new User();
        user1.setId(1);;
        user1.setName("张三");
        userDao.add(user1);
        userDao.delete(user1);
        List<User> userList = userDao.getAll();
        assertTrue(userList.isEmpty());
        assertFalse(userList.contains(user1));

    }

    @Test
    public void add() {

        UserDao userDao = new UserDao();
        User user1 = new User();
        user1.setId(1);;
        user1.setName("张三");
        userDao.add(user1);
        List<User> userList = userDao.getAll();
        assertTrue(userList.contains(user1));
    }
    @Test
    public void update(){
        User user = new User();
        User user1 = new User();
        User user2 = new User();
        user.setId(1);
        user.setName("张三");
        user1.setId(2);
        user1.setName("李四");
        user2.setId(3);
        user2.setName("王五");
        UserDao userDao = new UserDao();
        userDao.add(user);
        userDao.add(user1);
        userDao.add(user2);
        List<User> userList = userDao.getAll();
        userDao.delete(user1);
        System.out.println(userList);
        for (User u : userList) {
            if (u.getId()==1){
                u.setName("赵六");
                userDao.update(u.getId(),u);
            }
        }
        System.out.println(userList);
        assertTrue(userList.get(0).getName().equals("赵六"));
    }


    @Test
    public void update1(){
        User user = new User();
        User user1 = new User();
        User user2 = new User();
        user.setId(1);
        user.setName("张三");
        user1.setId(2);
        user1.setName("李四");
        user2.setId(3);
        user2.setName("王五");
        UserDao userDao = new UserDao();
        userDao.map.put(user.getId(),user.getName());
        userDao.map.put(user1.getId(),user1.getName());
        userDao.map.put(user2.getId(),user2.getName());
        Set<Integer> set = userDao.map.keySet();
        for (Integer i : set) {
            if (i==1){
                userDao.update1(i,"洪七");
            }
        }assertTrue(userDao.map.get(1).equals("洪七"));
    }
    @Test
    public void update2(){
        User user = new User();
        User user1 = new User();
        User user2 = new User();
        user.setId(1);
        user.setName("张三");
        user1.setId(2);
        user1.setName("李四");
        user2.setId(3);
        user2.setName("王五");
        UserDao userDao = new UserDao();
        userDao.map.put(user.getId(),user.getName());
        userDao.map.put(user1.getId(),user1.getName());
        userDao.map.put(user2.getId(),user2.getName());
        Set<Integer> set = userDao.map.keySet();
        userDao.update2();
        assertTrue(userDao.map.get(1).equals("速八"));
    }
}